﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WeddingEventManagementApp
{
    public partial class OutstandingBalancesReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SetReportParams();
                SetupReport();
            }
        }

        #region params

        private void GetClientNames(ref DataTable dataTable)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCreativeSystems"].ConnectionString))
            {
                con.Open();
                SqlDataAdapter adapter = new SqlDataAdapter("sp_getClientNames", con);
                adapter.Fill(dataTable);
            }
        }

        private void SetReportParams()
        {
            txtStartDate.Text = GetDefaultDate("start").ToShortDateString();
            txtEndDate.Text = GetDefaultDate("end").ToShortDateString();

            DataTable clientNames = new DataTable("ClientNames");
            GetClientNames(ref clientNames);
            DataRow newClientRow = clientNames.NewRow();
            newClientRow["Client Name"] = "All Clients";
            clientNames.Rows.InsertAt(newClientRow, 0);

            drpClientName.DataSource = clientNames;
            drpClientName.DataTextField = "Client Name";
            drpClientName.DataValueField = "Client Name";
            drpClientName.DataBind();
        }

        private DateTime GetDefaultDate(string startEnd)
        {
            DateTime currentDate = DateTime.Now;
            if (startEnd == "start")
            {
                return new DateTime(currentDate.Year, 01, 01);
            }
            else if (startEnd == "end")
            {
                return new DateTime(currentDate.Year, 12, 31);
            }
            return DateTime.Now;
        }

        #endregion

        private void SetupReport()
        {
            ReportViewer1.ProcessingMode = ProcessingMode.Local;
            ReportViewer1.ShowParameterPrompts = true;
            LocalReport localReport = ReportViewer1.LocalReport;
            localReport.ReportPath = "Reports/OutstandingBalances.rdlc";
            
            ReportParameter namesParam = new ReportParameter();
            namesParam.Name = "ClientName";
            namesParam.Values.Add(" ");
            namesParam.Visible = true;
            localReport.SetParameters(new ReportParameter[] { namesParam });

            RefreshReport(localReport);
        }

        private void RefreshReport(LocalReport localReport)
        {
            DateTime dueDateStartRange = DateTime.Parse(txtStartDate.Text); //new DateTime(2012, 01, 01);
            DateTime dueDateEndRange = DateTime.Parse(txtEndDate.Text); //new DateTime(2012, 12, 31);


            string clientName;
            if (drpClientName.SelectedItem == null || drpClientName.SelectedItem.Value == "All Clients")
            {
                clientName = null;
            }
            else
            {
                clientName = drpClientName.SelectedItem.Value;
            }

            DataTable dataTable1 = new DataTable("DataSet1");
            GetDataSet1(dueDateStartRange, dueDateEndRange, clientName, ref dataTable1);

            ReportDataSource rds1 = new ReportDataSource();
            rds1.Name = "DataSet1";
            rds1.Value = dataTable1;
            localReport.DataSources.Add(rds1);

            localReport.Refresh();
        }

        private void GetDataSet1(DateTime dueDateStartRange, DateTime dueDateEndRange, string clientName, ref DataTable dataTable)
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCreativeSystems"].ConnectionString))
            {
                con.Open();
                
                SqlDataAdapter adapter = new SqlDataAdapter("exec sp_OutstandingBalances @DueDateStartRange, @DueDateEndRange, @ClientName", con);
                adapter.SelectCommand.Parameters.AddWithValue("@DueDateStartRange", dueDateStartRange);
                adapter.SelectCommand.Parameters.AddWithValue("@DueDateEndRange", dueDateEndRange);
                if (clientName == null)
                {
                    adapter.SelectCommand.Parameters.AddWithValue("@ClientName", DBNull.Value);
                }
                else
                {
                    adapter.SelectCommand.Parameters.AddWithValue("@ClientName", clientName);
                }

                adapter.Fill(dataTable);
            }
        }

        protected void btnViewReport_Click(object sender, EventArgs e)
        {
            LocalReport localReport = ReportViewer1.LocalReport;
            localReport.DataSources.Clear();
            RefreshReport(localReport);
        }
    }
}